package optiondialoges;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;


import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.Serializable;
import javax.swing.JComboBox;

import panel.GlyphPanel;

public class ConvolveOptionDialog extends JDialog implements ActionListener,
		WindowListener, Serializable {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel buttonPanel = null;
	private JPanel optionPanel = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JTextArea kernelTextArea = null;
	private JCheckBox normalizeCheckBox = null;
	private GlyphPanel panel;
	private String kernel;
	private boolean normalize;
	private JComboBox kernelComboBox = null;

	/**
	 * @param owner
	 */
	public ConvolveOptionDialog(Frame owner, GlyphPanel panel) {
		super(owner);
		this.panel = panel;
		this.addWindowListener(this);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(266, 261);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getButtonPanel(), BorderLayout.SOUTH);
			jContentPane.add(getOptionPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes buttonPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.insets = new Insets(0, 10, 10, 10);
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.insets = new Insets(0, 10, 10, 10);
			buttonPanel = new JPanel();
			buttonPanel.setLayout(new GridBagLayout());
			buttonPanel.add(getOkButton(), gridBagConstraints1);
			buttonPanel.add(getCancelButton(), gridBagConstraints);
		}
		return buttonPanel;
	}

	/**
	 * This method initializes optionPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getOptionPanel() {
		if (optionPanel == null) {
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints4.gridy = 0;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.insets = new Insets(10, 0, 10, 0);
			gridBagConstraints4.gridx = 0;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.insets = new Insets(00, 10, 10, 10);
			gridBagConstraints3.gridy = 2;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.BOTH;
			gridBagConstraints2.gridy = 1;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.weighty = 1.0;
			gridBagConstraints2.insets = new Insets(10, 10, 10, 10);
			gridBagConstraints2.gridx = 0;
			optionPanel = new JPanel();
			optionPanel.setLayout(new GridBagLayout());
			optionPanel.add(getKernelTextArea(), gridBagConstraints2);
			optionPanel.add(getNormalizeCheckBox(), gridBagConstraints3);
			optionPanel.add(getKernelComboBox(), gridBagConstraints4);
		}
		return optionPanel;
	}

	/**
	 * This method initializes okButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("Ok");
			okButton.addActionListener(this);
		}
		return okButton;
	}

	/**
	 * This method initializes cancelButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(this);
		}
		return cancelButton;
	}

	/**
	 * This method initializes kernelTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getKernelTextArea() {
		if (kernelTextArea == null) {
			kernelTextArea = new JTextArea();
			kernelTextArea
					.setText("-1 -1 -1 -1 -1\n-1 -1 -1 -1 -1\n-1 -1 24 -1 -1\n-1 -1 -1 -1 -1\n-1 -1 -1 -1 -1");
			kernelTextArea.setEditable(false);
		}
		return kernelTextArea;
	}

	/**
	 * This method initializes normalizeCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getNormalizeCheckBox() {
		if (normalizeCheckBox == null) {
			normalizeCheckBox = new JCheckBox();
			normalizeCheckBox.setText("Normalize Kernel");
		}
		return normalizeCheckBox;
	}

	private String getKernel() {
		String k = kernelTextArea.getText();
		System.out.println(k);
		String[] rows = k.split("\r|\n|\r\n");
		k = "";
		for (String string : rows) {
			k += string + "\\n";
		}
		return k;
	}

	/**
	 * This method initializes kernelComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getKernelComboBox() {
		if (kernelComboBox == null) {
			kernelComboBox = new JComboBox();
			kernelComboBox.setPreferredSize(new Dimension(120, 25));
			kernelComboBox.addItem("Default");
			kernelComboBox.addItem("Roberts x");
			kernelComboBox.addItem("Roberts y");
			kernelComboBox.addItem("Prewitt x");
			kernelComboBox.addItem("Prewitt y");
			kernelComboBox.addItem("Sobel x");
			kernelComboBox.addItem("Sobel y");
			kernelComboBox.addItem("Frei-Chen x");
			kernelComboBox.addItem("Frei-Chen y");
			kernelComboBox.addItem("Custom");
			kernelComboBox.setSelectedIndex(0);
			kernelComboBox.addActionListener(this);
		}
		return kernelComboBox;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof JButton) {
			JButton button = (JButton) e.getSource();
			if (button.getText().contentEquals("Ok")) {
				kernel = kernelTextArea.getText();
				normalize = normalizeCheckBox.isSelected();
				String command = String.format(
						"run(\"Convolve...\", \"text1=[%s]", getKernel());
				if (normalize) {
					command = String.format("%s normailze", command);
				}
				command += "\");";
				panel.setOptionsSet(true);
				panel.setDialog(this);
				panel.setCommand(command);
			} else {
				kernelTextArea.setText(kernel);
				normalizeCheckBox.setSelected(normalize);
			}
			this.setVisible(false);
		} else if (e.getSource() instanceof JComboBox) {
			JComboBox comboBox = (JComboBox) e.getSource();
			String selected = (String) comboBox.getSelectedItem();
			if (selected.contentEquals("Custom")) {
				kernelTextArea.setEditable(true);
			} else {
				kernelTextArea.setEditable(false);
			}
			if (selected.contentEquals("Default")) {
				kernelTextArea
						.setText("-1 -1 -1 -1 -1\n-1 -1 -1 -1 -1\n-1 -1 24 -1 -1\n-1 -1 -1 -1 -1\n-1 -1 -1 -1 -1");
			} else if (selected.contentEquals("Roberts x")) {
				kernelTextArea.setText("-1 0 0\n0 1 0\n0 0 0\n");
			} else if (selected.contentEquals("Roberts y")) {
				kernelTextArea.setText("0 0 -1\n0 1 0\n0 0 0\n");
			} else if (selected.contentEquals("Prewitt x")) {
				kernelTextArea.setText("1 0 -1\n1 0 -1\n1 0 -1\n");
			} else if (selected.contentEquals("Prewitt y")) {
				kernelTextArea.setText("-1 -1 -1\n0 0 0\n1 1 1\n");
			} else if (selected.contentEquals("Sobel y")) {
				kernelTextArea.setText("-1 -2 -1\n0 0 0\n1 2 1\n");
			} else if (selected.contentEquals("Sobel x")) {
				kernelTextArea.setText("1 0 -1\n2 0 -2\n1 0 -1\n");
			} else if (selected.contentEquals("Frei-Chen x")) {
				kernelTextArea.setText("1 0 -1\n1.4142 0 -1.4142\n1 0 -1\n");
			} else if (selected.contentEquals("Frei-Chen y")) {
				kernelTextArea.setText("-1 -1.4142 -1\n0 0 0\n1 1.4142 1\n");
			}

		}
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		kernelTextArea.setText(kernel);
		normalizeCheckBox.setSelected(normalize);

	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent e) {
		kernel = kernelTextArea.getText();
		normalize = normalizeCheckBox.isSelected();

	}

} // @jve:decl-index=0:visual-constraint="10,10"
